/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-07-01 17:12:21
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-07-01 18:03:00
 * @FilePath: \keep_motion_api\models\sys\file.model.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const { fileModules } = require('@/utils/model-mapping');

// 文件元数据模型
const fileSchema = new Schema({
  filename: String,
  originalname: String,
  mimetype: String,
  size: Number,
  storage_path: String,
  url: String,
  module: {
    type: String,
    enum: fileModules,
    required: true
  },
  ref_id: {
    type: Schema.Types.ObjectId,
    refPath: 'module',
    index: true
  },
  creator: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
  created_at: { type: Date, default: Date.now }
});

// 添加静态方法
fileSchema.statics.getModelName = function(module) {
  const { getModel } = require('@/utils/model-mapping');
  return getModel(module);
};

// 添加实例方法
fileSchema.methods.getRefModel = function() {
  const modelName = this.constructor.getModelName(this.module);
  return mongoose.model(modelName);
};


module.exports = mongoose.model('File', fileSchema);
